package com.dy.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dy.dto.WagesAddDTO;
import com.dy.dto.WagesExcelDTO;
import com.dy.dto.WagesPageDTO;
import com.dy.dto.WagesUpdateDTO;
import com.dy.service.WagesService;
import com.dy.utils.ApiResult;
import com.dy.utils.ExcelPoiUtils;
import com.dy.vo.WagesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: dy
 * @date: 2024/5/7 14:33
 * @ClassDescription:
 */
@RestController
@RequestMapping("/wages")
@Api(tags = "工资管理")
public class WagesController {

    @Resource
    private WagesService wagesService;

    @GetMapping("/pageList")
    @ApiOperation("分页查询")
    public ApiResult<IPage<WagesVO>> pageList(WagesPageDTO dto){
        return ApiResult.ok(
                wagesService.pageList(dto)
        );
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public ApiResult<Boolean> add(@Valid @RequestBody WagesAddDTO dto){
        return ApiResult.ok(
                wagesService.add(dto)
        );
    }

    @GetMapping("/getWagesById/{id}")
    @ApiOperation("详情")
    public ApiResult<WagesVO> getWagesById(@PathVariable("id")Long id){
        return ApiResult.ok(
                wagesService.getWagesById(id)
        );
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("删除")
    public ApiResult<Boolean> deleteWagesById(@PathVariable("id")Long id){
        return ApiResult.ok(
                wagesService.deleteWagesById(id)
        );
    }

    @PutMapping("/update")
    @ApiOperation("更新修改")
    public ApiResult<Boolean> update(@Valid @RequestBody WagesUpdateDTO dto){
        return ApiResult.ok(
                wagesService.updateWages(dto)
        );
    }

    @GetMapping("/excelDownload")
    @ApiOperation("excel模板下载")
    public void excelDownload(HttpServletResponse response) throws IOException {
        List<WagesExcelDTO> list = new ArrayList<>();
        WagesExcelDTO wagesExcelDTO = new WagesExcelDTO();
        wagesExcelDTO.setRecordDate("2024-05-09");
        list.add(wagesExcelDTO);
        ExcelPoiUtils.exportExcel(list,"工资列表","工资列表",WagesExcelDTO.class,"工资列表导入模板",response);
    }

    @GetMapping("/excelExport")
    @ApiOperation("excel数据导出")
    public void excelExport(HttpServletResponse response,WagesPageDTO dto) throws IOException {
        List<WagesExcelDTO> list = wagesService.getList(dto);
        ExcelPoiUtils.exportExcel(list,"工资列表","工资列表",WagesExcelDTO.class,"工资列表",response);
    }

    @PostMapping("/excelImport")
    @ApiOperation("excel数据导入")
    public void excelImport(@RequestPart("file") MultipartFile file) throws IOException {
        ExcelImportResult<WagesExcelDTO> result = ExcelPoiUtils.importExcelMore(file.getInputStream(), 1, 1, WagesExcelDTO.class);
        List<WagesExcelDTO> wagesExcelDtoList = result.getList();
        wagesService.excelImport(wagesExcelDtoList);
    }

}
